package com.transaction.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.config.R;
import com.transaction.domain.SysJob;
import com.transaction.domain.SysSenior;
import com.transaction.domain.SysSeniorJob;
import com.transaction.service.SysJobService;
import com.transaction.service.SysSeniorJobService;
import com.transaction.service.SysSeniorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("senior")
public class SysSeniorController {

    @Autowired
    SysSeniorService sysSeniorService;
    @Autowired
    SysJobService sysJobService;
    @Autowired
    SysSeniorJobService sysSeniorJobService;

    /**
     * 查询列表
     * @param sysSenior
     * @parampageBean
     * @return
     */
    @PostMapping("seniorList")
    public R sysSeniorList(@RequestBody SysSenior sysSenior){
        System.err.println(sysSenior);
        //        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.like("jobName",sysSenior.getJobName());
        PageHelper.startPage(sysSenior.getPageNum(),sysSenior.getPageSize());
        List<SysSenior> list = sysSeniorService.objectList(sysSenior);

        return R.successed(new PageInfo<>(list));
    }

    /**
     * 查询职务
     * @return
     */
    @GetMapping("jobList")
    public R jobList(){
        return R.successed(sysJobService.list());
    }

    /**
     * 添加修改
     * @return
     */
    @PostMapping("addsenior")
    public R addsenior(@RequestBody SysSenior sysSenior){
        if (sysSenior.getSeniorId()==null){
            sysSenior.setTime(new Date());
            sysSeniorService.save(sysSenior);
            String[] jobId = sysSenior.getJobId();
            for (String s : jobId) {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("job_name",s);
                SysJob one = sysJobService.getOne(queryWrapper);
                SysSeniorJob sysSeniorJob = new SysSeniorJob();
                sysSeniorJob.setJobId(one.getJobId());
                sysSeniorJob.setSeniorId(sysSenior.getSeniorId());
                sysSeniorJobService.save(sysSeniorJob);
            }
            return R.successed("添加成功");
        }else{
            sysSeniorService.updateById(sysSenior);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("senior_id",sysSenior.getSeniorId());
            sysSeniorJobService.remove(queryWrapper);

            String[] jobId = sysSenior.getJobId();
            for (String s : jobId) {
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("job_name",s);
                SysJob one = sysJobService.getOne(queryWrapper1);
                if (ObjectUtil.isEmpty(one)){
                    System.err.println(one);
                }else {
                    SysSeniorJob sysSeniorJob = new SysSeniorJob();
                    sysSeniorJob.setSeniorId(sysSenior.getSeniorId());
                    sysSeniorJob.setJobId(one.getJobId());
                    sysSeniorJobService.save(sysSeniorJob);
                }


            }
      return R.successed("修改成功");

        }
    }
}
